import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import { NavParams } from 'ionic-angular';
import {ContactPage} from '../contact';
//三级跳转
import {inexistDealerPage} from './inexistDealer/inexistDealer'
import {existDealerPage} from './existDealer/existDealer'
//  http请求
import {Http,Headers} from '@angular/http';
import {interfaceUrls} from '../../../provider/serverUrls';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';
//通过手机号查询到的数据传递到已存在的经销商页面
@Component({
    selector: 'page-addTrench', 
    templateUrl: 'addTrench.html'
})
export class addTrenchPage {
    //需要在另外一个页面传给后台，组件间数据传递
    trenchName;
    trenchMobile;
    //http请求
	a=1
	product:any;
	isShow=false;
    private headers = new Headers({'Content-Type':'application/json;chartset=utf-8'});

    param = {
        "name": this.trenchName,
        "mobile": this.trenchMobile
    };
    infoData = {};
    constructor(
    	public navCtrl: NavController,
    	public http:Http,
    	public params:NavParams
    	) {
    	this.product = this.params.get('product')
    	console.info("添加页面接受产品信息的值+",this.product)
    }
	addBack(){
		 this.navCtrl.pop();
	}
    addNext(){
    	if(this.trenchName==undefined||this.trenchMobile==undefined){
    		this.isShow=true;
    		console.log(this.trenchName,this.trenchMobile)
    	}else{
			console.log(this.trenchName,this.trenchMobile)
    		console.log("添加经销商++++此经销商存在")
    		   this.http
        		.post(interfaceUrls.infoSearch, 	
        			{
				        "name": this.trenchName,
				        "mobile": this.trenchMobile
				    }
        			, {headers: this.headers})
        		.timeout(2000)
            	.toPromise()
            	.then((response) => {
            		if(this.infoData!={}){
            			this.infoData = response.json().infoData;
			            console.info(this.infoData);
			            this.navCtrl.push(existDealerPage,
			            	{
			                	data: this.infoData,
				                product:this.product,
				                trenchName:this.trenchName,
				                trenchMobile:this.trenchMobile
	                		}
			            );
            		}else{
            			this.infoData = response.json().infoData;
		                console.info(this.infoData);
		                this.navCtrl.push(inexistDealerPage,
		                    {
		                        data: this.infoData,
		                        product:this.product,
		                        trenchName:this.trenchName,
		                  		trenchMobile:this.trenchMobile
		                    }
		                  
		                );
            		}
	                
	            })
	            .catch( err => {
	                console.log('err');
	            })	
	            
    			}
     
    }
    
    
    
    
}